package com.inspector.app.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import com.inspector.app.BuildConfig;

import java.math.BigDecimal;
import java.math.RoundingMode;


/**
 *
 */
public class CommonUtil {


    /**
     * 当前是否是鸿蒙系统
     * @return
     */
    public static boolean isHarmonyOs() {
        try {
            if (!(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)) { //[鸿蒙系统没有 Android 10 以下的]
                return false;
            }
            Class<?> buildExClass = Class.forName("com.huawei.system.BuildEx");
            Object osBrand = buildExClass.getMethod("getOsBrand").invoke(buildExClass);
            if (osBrand == null) {
                return false;
            }
            return "harmony".equalsIgnoreCase(osBrand.toString());
        } catch (Throwable e) {
            return false;
        }
    }


    /**
     * 判断是否平板设备
     * true:平板,false:手机
     */
    public static boolean isTabletDevice(Context context) {
        return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >=
                Configuration.SCREENLAYOUT_SIZE_LARGE;
    }


    @SuppressLint("NonConstantResourceId")
    public static String getDistanceUnit() {
        /*switch (ConstantUtil.Unit_Distance) {
            case R.id.rb_unit_distance_nm:
                return "nm";

            case R.id.rb_unit_distance_mi:
                return "mi";

            default:
                return "km";
        }*/
        // TODO: 2024/7/4
        return "km";
    }

    //[double保留有效数字]
    public static String formatDouble(double d, int scale) {
        try {
            return new BigDecimal(d).setScale(scale, RoundingMode.DOWN).stripTrailingZeros().toPlainString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 设置中 速度转换
     * 米每秒转换成设置的速度显示
     * https://zh.wikipedia.org/zh-hans/%E7%AF%80_(%E5%96%AE%E4%BD%8D)
     * https://www.sojson.com/convert/speed.html
     */
    public static String speedConversion(double speedMS) {
        // TODO: 2024/7/5
        /*if (ConstantUtil.SPEED_TYPE == SpeedType.KN) {
            return CommonUtil.formatDouble(speedMS * 1.943844, 1);
        } else if (ConstantUtil.SPEED_TYPE == SpeedType.MILES_H) {
            return CommonUtil.formatDouble(speedMS * 2.236936, 1);
        } else {
            return CommonUtil.formatDouble(speedMS * 3.6, 1);
        }*/

        return CommonUtil.formatDouble(speedMS * 3.6, 1);
    }

    public static String getSpeedUnit() {
        // TODO: 2024/7/5
        /*if (ConstantUtil.SPEED_TYPE == SpeedType.KN) {
            return "kn";
        } else if (ConstantUtil.SPEED_TYPE == SpeedType.MILES_H) {
            return "mph";
        } else {
            return "km/h";
        }*/

        return "km/h";
    }

    public static String getFormatLatLng(double latLng, String defaultContent) {
        try {
            // TODO: 2024/7/8
            /*if (ConstantUtil.LOCATION_TYPE == LocationType.DEGRESS_AND_SECONDS) {
                return LocationUtils.D2Dms(latLng);
            } else if (ConstantUtil.LOCATION_TYPE == LocationType.DEGRESS) {
                return LocationUtils.D2Dm(latLng);
            } else {
                return CommonUtil.formatDouble(latLng, 4);
            }*/

            return CommonUtil.formatDouble(latLng, 4);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return defaultContent;
    }


    //[去除小数点后无效0数字]
    public static String convertByBigDecimal(String value) {
        try {
            if (TextUtils.isEmpty(value)) return "";
            BigDecimal bigDecimal = new BigDecimal(value);
            BigDecimal noZeros = bigDecimal.stripTrailingZeros();
            return noZeros.toPlainString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }


    public static void hideKeyboard(Activity activity) {
        try {
            InputMethodManager inputManager = (InputMethodManager) activity
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            View currentFocusedView = activity.getCurrentFocus();
            if (currentFocusedView == null) {
                currentFocusedView = activity.getWindow().getDecorView();
            }
            if (currentFocusedView != null) {
                inputManager.hideSoftInputFromWindow(currentFocusedView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                currentFocusedView.clearFocus();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void hideKeyboardWithOutClearFocus(Fragment fragment) {
        try {
            FragmentActivity activity = fragment.requireActivity();
            InputMethodManager inputManager = (InputMethodManager) activity
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            View currentFocusedView = activity.getCurrentFocus();
            if (currentFocusedView == null) {
                currentFocusedView = activity.getWindow().getDecorView();
            }
            if (currentFocusedView != null) {
                inputManager.hideSoftInputFromWindow(currentFocusedView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
//                currentFocusedView.clearFocus();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void hideKeyboard(Fragment fragment) {
        try {
            FragmentActivity activity = fragment.requireActivity();
            InputMethodManager inputManager = (InputMethodManager) activity
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            View currentFocusedView = activity.getCurrentFocus();
            if (currentFocusedView == null) {
                currentFocusedView = activity.getWindow().getDecorView();
            }
            if (currentFocusedView != null) {
                inputManager.hideSoftInputFromWindow(currentFocusedView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                currentFocusedView.clearFocus();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void hideKeyboard(Dialog dialog) {
        try {
            if (dialog == null) return;
            InputMethodManager inputManager = (InputMethodManager) dialog.getContext()
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            View currentFocusedView = dialog.getCurrentFocus();
            if (currentFocusedView != null) {
                inputManager.hideSoftInputFromWindow(currentFocusedView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                currentFocusedView.clearFocus();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
